﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string keyName = "HKEY_CURRENT_USER\\SoftWare\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

            textBox1.Text = textBox6.Text + Regex.Replace(textBox1.Text, @"^.*\\+", "\\");
            textBox2.Text = textBox6.Text + Regex.Replace(textBox2.Text, @"^.*\\+", "\\");
            textBox3.Text = textBox6.Text + Regex.Replace(textBox3.Text, @"^.*\\+", "\\");
            textBox4.Text = textBox6.Text + Regex.Replace(textBox4.Text, @"^.*\\+", "\\");
            textBox5.Text = textBox6.Text + Regex.Replace(textBox5.Text, @"^.*\\+", "\\");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox6.Text = "D:\\" + Environment.UserName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //应用新的路径
            try
            {
                if (!Directory.Exists(textBox1.Text)) Directory.CreateDirectory(textBox1.Text);
                if (!Directory.Exists(textBox2.Text)) Directory.CreateDirectory(textBox2.Text);
                if (!Directory.Exists(textBox3.Text)) Directory.CreateDirectory(textBox3.Text);
                if (!Directory.Exists(textBox4.Text)) Directory.CreateDirectory(textBox4.Text);
                if (!Directory.Exists(textBox5.Text)) Directory.CreateDirectory(textBox5.Text);

                Registry.SetValue(keyName, "Desktop", textBox1.Text);
                Registry.SetValue(keyName, "Personal", textBox2.Text);
                Registry.SetValue(keyName, "Cache", textBox3.Text);
                Registry.SetValue(keyName, "Favorites", textBox4.Text);
                Registry.SetValue(keyName, "Templates", textBox5.Text);

                RestartExplorer();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", 0, MessageBoxIcon.Warning);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //恢复默认
            try
            {
                textBox6.Text = Environment.GetEnvironmentVariable("USERPROFILE");

                Registry.SetValue(keyName, "Desktop", "%USERPROFILE%\\Desktop");
                Registry.SetValue(keyName, "Personal", "%USERPROFILE%\\My Documents");
                Registry.SetValue(keyName, "Cache", "%USERPROFILE%\\Temporary Internet Files");
                Registry.SetValue(keyName, "Favorites", "%USERPROFILE%\\Favorites");
                Registry.SetValue(keyName, "Templates", "%USERPROFILE%\\Templates");

                RestartExplorer();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", 0, MessageBoxIcon.Warning);
            }

        }
        private void RestartExplorer()
        {
            try
            {
                var p = Process.GetProcessesByName("explorer")[0];
                p.Kill();
                var file = p.MainModule.FileName;
                Process.Start(file);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", 0, MessageBoxIcon.Warning);
            }
        }
    }
}
